﻿using GestionCompte.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using System.IO;
using System.Xml;
using System.Text;
using System.Configuration;

namespace GestionCompte.Helpers
{
    public static class MenuHelper
    {
        public static HtmlString GenerateMenu(this HtmlHelper htmlHelper, string Type, string FileMenuName = "", List<CategoryEntity> categ = null, string classToApply = "", bool isAjaxMenu = false, string AjaxUrl= "", bool GenerateJS = true)
        {
            MenuItemListEntity ItemList;

            if (Type.Equals("file"))
            {
                //Create the Xml Document
                XDocument xmlDoc = XDocument.Load(ConfigurationManager.AppSettings.Get("FilesMenuDirectory") + FileMenuName);

                //Create and fill the menuList from xml file
                List<MenuItemEntity> menuList = new List<MenuItemEntity>();
                menuList.AddRange(xmlDoc.Descendants("MenuItem").Select(x => new MenuItemEntity(x.Attribute(XName.Get("DisplayName")).Value, 
                    x.Attribute(XName.Get("HtmlId")).Value, 
                    bool.Parse(x.Attribute(XName.Get("IsActivated")).Value), 
                    GestionCompte.Enums.MenuItemCategories.Layout,
                    x.Attribute(XName.Get("Controller")).Value, 
                    x.Attribute(XName.Get("Action")).Value)));

                //create the itemList
                ItemList = new MenuItemListEntity(menuList);
            }
            else
            {
                //Create and fill the menuList from xml file
                List<MenuItemEntity> menuList = new List<MenuItemEntity>();
                menuList.AddRange(categ.Select(x => new MenuItemEntity(x)));

                //create the itemList
                ItemList = new MenuItemListEntity(menuList);
            }

            //Create the string for teh script tag.
            StringBuilder idsString = new StringBuilder("");
            //create the li tags from the menu item list
            List<TagBuilder> listLiTags = new List<TagBuilder>();
            foreach (MenuItemEntity m in ItemList)
            {
                if (m.IsActivated)
                {
                    TagBuilder tb = new TagBuilder("li");
                    tb.MergeAttribute("id", m.HtmlId);
                    tb.InnerHtml = m.DisplayName;
                    listLiTags.Add(tb);
                }
            }

            //Create the ul tag that will contains the li tags 
            TagBuilder ulTag = new TagBuilder("ul");
            ulTag.MergeAttribute("class", classToApply);
            foreach (TagBuilder tb in listLiTags)
            {
                ulTag.InnerHtml += tb.ToString(TagRenderMode.Normal);
            }

            //Generate the script tag for the menu
            TagBuilder scriptTag = new TagBuilder("script");
            scriptTag.MergeAttribute("type", "text/javascript");

            scriptTag.InnerHtml = "";

            if (GenerateJS)
            {
                foreach (MenuItemEntity m in ItemList)
                {
                    if (isAjaxMenu)
                    {
                        AjaxUrl = AjaxUrl.Remove(AjaxUrl.LastIndexOf('/'));
                        AjaxUrl += "/" + m.Action;
                        scriptTag.InnerHtml += @"$('#" + m.HtmlId + @"').click(function () {
                        AjaxCallPage('" + AjaxUrl + @"');
                    });";
                    }
                    else
                    {
                        scriptTag.InnerHtml += @"$('#" + m.HtmlId + @"').click(function () {
                        $('#menuForm').attr('action', '/' + '" + m.Controller + @"' + '/' + '" + m.Action + @"');
                        $('#menuForm').submit();
                        });";
                    }


                }
            }
            //Return the html string that will contains the ul tag and the script

            if (GenerateJS)
                return new HtmlString(ulTag.ToString(TagRenderMode.Normal) + scriptTag.ToString(TagRenderMode.Normal));
            else
                return  new HtmlString(ulTag.ToString(TagRenderMode.Normal));
        }

        public static HtmlString GenerateHomeMosaic(this HtmlHelper htmlHelper)
        {
            //Create the Xml Document
            XDocument xmlDoc = XDocument.Load(ConfigurationManager.AppSettings.Get("FilesMenuDirectory") + "MenuLayout.xml");

            //Create and fill the menuList from xml file
            List<MenuItemEntity> menuList = new List<MenuItemEntity>();
            menuList.AddRange(xmlDoc.Descendants("MenuItem").Select(x => new MenuItemEntity(x.Attribute(XName.Get("DisplayName")).Value,
                x.Attribute(XName.Get("HtmlId")).Value,
                bool.Parse(x.Attribute(XName.Get("IsActivated")).Value),
                GestionCompte.Enums.MenuItemCategories.Layout,
                x.Attribute(XName.Get("Controller")).Value,
                x.Attribute(XName.Get("Action")).Value)));

            //create the itemList
            MenuItemListEntity ItemList = new MenuItemListEntity(menuList);

            string ResultString = "";

            foreach (MenuItemEntity m in ItemList)
            {
                TagBuilder spanTag = new TagBuilder("span"); 
                spanTag.MergeAttribute("id", m.HtmlId); 
                spanTag.MergeAttribute("class", "boxContainer");
                spanTag.InnerHtml = m.DisplayName; 

                ResultString += spanTag.ToString(TagRenderMode.Normal);
            }

            return new HtmlString(ResultString);
        }

    }
}